home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / pdflib / clients / pdfimage.c < prev    next >
C/C++ Source or Header  |  1999-12-06  |  5KB  |  177 lines

  1. /*---------------------------------------------------------------------------*
  2.  |        PDFlib - A library for dynamically generating PDF files            |
  3.  +---------------------------------------------------------------------------+
  4.  |        Copyright (c) 1997-1999 Thomas Merz. All rights reserved.          |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is not in the public domain.  It is subject to the       |
  7.  |    "Aladdin Free Public License".  See the file license.txt for details.  |
  8.  |    This license grants you the right to use and redistribute PDFlib       |
  9.  |    under certain conditions. Among other things, the license requires     |
  10.  |    that the copyright notice and this notice be preserved on all copies.  |
  11.  |    This requirement extends to ports to other programming languages.      |
  12.  |                                                                           |
  13.  |    In short, you are allowed to develop and use PDFlib-based software     |
  14.  |    as long as you don't sell it. Commercial use of PDFlib requires a      |
  15.  |    commercial license which can be obtained from the author of PDFlib.    |
  16.  |    Contact information can be found in the accompanying PDFlib manual.    |
  17.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  18.  |    however, will receive warranty and support statements in writing.      |
  19.  *---------------------------------------------------------------------------*/
  20.  
  21. /* imagepdf.c
  22.  *
  23.  * Convert TIFF/GIF/JPEG images to PDF
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31.  
  32. #if !defined(WIN32) && !defined(MAC)
  33. #include <unistd.h>
  34. #endif
  35.  
  36. #ifdef WIN32
  37. #include <process.h>
  38. #endif
  39.  
  40. #ifdef NeXT
  41. #include <libc.h>    /* for getopt(), optind, optarg */
  42. #endif
  43.  
  44. #ifdef __CYGWIN32__
  45. #include <getopt.h>    /* for getopt(), optind, optarg */
  46. #endif
  47.  
  48. #include "pdflib.h"
  49. #include "p_config.h"
  50.  
  51. static void
  52. usage(void)
  53. {
  54.     fprintf(stderr, "imagepdf - Convert ");
  55. #ifdef HAVE_LIBTIFF
  56.     fprintf(stderr, "TIFF/");
  57. #endif
  58.     fprintf(stderr, "GIF/JPEG images to PDF. (C) Thomas Merz 1997-99\n");
  59.     fprintf(stderr, "usage: imagepdf [options] imagefile(s)\n");
  60.     fprintf(stderr, "Available options:\n");
  61.     fprintf(stderr, "-a         ASCII mode (default: binary)\n");
  62.     fprintf(stderr, "-c         print caption\n");
  63.     fprintf(stderr, "-o <file>  output file\n");
  64.  
  65.     exit(1);
  66. }
  67.  
  68. /* Several magic numbers for image file formats */
  69.  
  70. #define    GIF_MAGIC    "GIF"
  71. #define TIFF_MAGIC_M    "MM"
  72. #define TIFF_MAGIC_I    "II"
  73. #define PNG_MAGIC    "\x89PNG"
  74. #define JPEG_MAGIC    "\xFF\xD8"
  75.  
  76. #define MAGIC_LEN_MAX    10        /* maximum length of magic strings */
  77.  
  78. int
  79. main(int argc, char *argv[])
  80. {
  81.     char    *pdffilename = NULL;
  82.     FILE    *imagefile;
  83.     PDF        *p;
  84.     int        image;
  85.     int        opt;
  86.     int        caption = 0;
  87.     float    scale;
  88.     char    header[MAGIC_LEN_MAX];
  89.     
  90.     while ((opt = getopt(argc, argv, "o:")) != -1)
  91.     switch (opt) {
  92.         case 'c':
  93.         caption = 1;
  94.         break;
  95.  
  96.         case 'o':
  97.         pdffilename = optarg;
  98.         break;
  99.     }
  100.  
  101.     if (optind == argc) {
  102.     fprintf(stderr, "Error: no image files given.\n");
  103.     usage();
  104.     }
  105.  
  106.     if (pdffilename == NULL) {
  107.     fprintf(stderr, "Error: no output file given.\n");
  108.     usage();
  109.     }
  110.  
  111.     p = PDF_new();
  112.  
  113.     PDF_open_file(p, pdffilename);
  114.  
  115.     if (p == NULL) {
  116.     fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename);
  117.     exit(1);
  118.     }
  119.  
  120.  
  121.     PDF_set_info(p, "Creator", "imagepdf");
  122.  
  123.     while (optind++ < argc) {
  124.     fprintf(stderr, "Processing image file %s...\n", argv[optind-1]);
  125.  
  126.     if ((imagefile = fopen(argv[optind-1], READMODE)) == NULL) {
  127.        fprintf(stderr, "Error: Couldn't open image file %s - skipped.\n",
  128.         argv[optind-1]);
  129.        continue;
  130.     }
  131.  
  132.     if (fread(header, MAGIC_LEN_MAX, 1, imagefile) != 1)  {
  133.        fprintf(stderr, "Error: Couldn't read from image file %s - skipped.\n",
  134.         argv[optind-1]);
  135.        fclose(imagefile);
  136.        continue;
  137.     }
  138.     fclose(imagefile);
  139.  
  140.     if (!strncmp(header, GIF_MAGIC, strlen(GIF_MAGIC)))
  141.        image = PDF_open_GIF(p, argv[optind-1]);
  142.  
  143. #ifdef HAVE_LIBTIFF
  144.     else if (!strncmp(header, TIFF_MAGIC_I, strlen(TIFF_MAGIC_I)) ||
  145.              !strncmp(header, TIFF_MAGIC_M, strlen(TIFF_MAGIC_M)))
  146.        image = PDF_open_TIFF(p, argv[optind-1]);
  147. #endif
  148.  
  149.     else if (!strncmp(header, JPEG_MAGIC, strlen(JPEG_MAGIC)))
  150.        image = PDF_open_JPEG(p, argv[optind-1]);
  151.  
  152.     else
  153.        image = -1;    /* unknown file type */
  154.  
  155.     if (image == -1) {
  156.         fprintf(stderr,"Error: Couldn't analyze image %s - skipped.\n",
  157.                 argv[optind-1]);
  158.         continue;
  159.     }
  160.  
  161.     scale = 1.0;
  162.  
  163.     PDF_begin_page(p, PDF_get_image_width(p, image) * scale, 
  164.             PDF_get_image_height(p, image)* scale);
  165.     
  166.     /* define outline with filename */
  167.     PDF_add_bookmark(p, argv[optind-1], 0, 0);
  168.  
  169.     PDF_place_image(p, image, 0.0, 0.0, scale);
  170.  
  171.     PDF_end_page(p);
  172.     }
  173.  
  174.     PDF_close(p);
  175.     exit(0);
  176. }
  177.